from PySide6.QtWidgets import (QApplication, QMainWindow, QTextEdit,QLineEdit,
                               QDockWidget, QListWidget, QPlainTextEdit)
from PySide6.QtCore import Qt, QSettings
import sys

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("DockWidget Demo")
        self.resize(900, 600)

        # ---- 中央部件 ----
        self.editor = QTextEdit()
        self.setCentralWidget(self.editor)

        # ---- 左侧 Dock ----
        self.list_dock = QDockWidget("文件列表", self)
        self.file_list = QListWidget()
        self.file_list.addItems([f"文件{i}.py" for i in range(20)])
        self.list_dock.setWidget(self.file_list)
        # 默认放在左侧，允许关闭/拖动/浮动
        self.addDockWidget(Qt.LeftDockWidgetArea, self.list_dock)

        # ---- 右侧 Dock ----
        self.log_dock = QDockWidget("日志", self)
        self.log_edit = QPlainTextEdit()
        self.log_edit.setPlainText("DockWidget 初始化完成\n")
        self.log_dock.setWidget(self.log_edit)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.log_dock)
        self.tabifyDockWidget(self.list_dock,self.log_dock)

        self.top_dock = QDockWidget("top", self)
        self.lineEdit = QLineEdit()
        self.top_dock.setWidget(self.lineEdit)
        self.addDockWidget(Qt.TopDockWidgetArea,self.top_dock)


        # ---- 停靠策略（可选） ----
        self.setDockOptions(QMainWindow.AllowNestedDocks |
                            QMainWindow.AllowTabbedDocks  |
                            QMainWindow.AnimatedDocks)

        # ---- 恢复上次布局 ----
        self.restore_dock_state()

    # ---------- 关闭时保存布局 ----------
    def closeEvent(self, event):
        settings = QSettings("MySoft", "DockDemo")
        settings.setValue("geometry", self.saveGeometry())
        settings.setValue("state", self.saveState())
        super().closeEvent(event)

    # ---------- 启动时恢复布局 ----------
    def restore_dock_state(self):
        settings = QSettings("MySoft", "DockDemo")
        self.restoreGeometry(settings.value("geometry", b""))
        self.restoreState(settings.value("state", b""))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    qss = """
    QTabBar::tab {
        background: #2d2d2d;
        color: #ffffff;
        padding: 6px 12px;
        margin-right: 1px;
    }
    QTabBar::tab:selected {
        background: #0078d4;
    }
    QTabBar::tab:!selected:hover {
        background: #444444;
    }
    """
    app.setStyleSheet(qss)
    win = MainWindow()
    win.show()
    sys.exit(app.exec())